//
//  KLYJDDetViewController.m
//  YaDiuDiu
//
//  Created by 欧洪飞 on 2017/10/28.
//  Copyright © 2017年 jy. All rights reserved.
//

#import "KLYJDDetViewController.h"

#import "KLYJDView.h"
#import "KLDjdManViewController.h"
@interface KLYJDDetViewController ()

@property (nonatomic,strong)NSMutableArray *array;//

@property (nonatomic,strong)NSDictionary *dic;//

@end

@implementation KLYJDDetViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = COLOR_RGB(242, 242, 242, 1);
    [self showNav:@"订单详情"];
    _array = [NSMutableArray array];
    
    [self loadData];
    
}

- (void)loadUI{
    UIScrollView *sclView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 64, ScreenWidth, ScreenHeight-64)];
    sclView.contentSize = CGSizeMake(ScreenWidth, 540);
    [self.view addSubview:sclView];
    
    KLYJDView *view = [[[NSBundle mainBundle] loadNibNamed:@"KLYJDView" owner:nil options:nil] lastObject];
    view.frame = CGRectMake(0, 0, ScreenWidth, 540);
    [sclView addSubview:view];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(click)];
    view.sclView.userInteractionEnabled = YES;
    [view.sclView addGestureRecognizer:tap];
    
    NSMutableArray *images = [NSMutableArray array];
    for (NSDictionary *dic in _array) {
        NSString *imgUrl = [NSString stringWithFormat:@"%@%@",[user objectForKey:@"ImageUrl"],[dic objectForKey:@"img"]];
        [images addObject:imgUrl];
    }
    
    for (NSInteger i = 0; i<images.count; i++) {
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(16*(i+1)+60*i, 20, 60, 60)];
        imageView.layer.cornerRadius = 30.0;
        imageView.layer.masksToBounds = YES;
        [imageView sd_setImageWithURL:[NSURL URLWithString:images[i]] placeholderImage:nil];
        [view.sclView addSubview:imageView];
        
    }
    
    view.labOrder.text = [_dic objectForKey:@"ordernumber"];
    NSString *date = [NSString stringWithFormat:@"%li",[[_dic objectForKey:@"addtime"] integerValue]];
    NSString *time = [self getNewDate:date];
    view.labDate.text = time;
    
    view.labKG.text = [_dic objectForKey:@"weight"];
    
    view.labQHDZ.text = [_dic objectForKey:@"quaddress"];
    view.labQHDet.text = [_dic objectForKey:@"quhousnum"];
    
    view.labPSDZ.text = [_dic objectForKey:@"goaddress"];
    view.labPSDet.text = [_dic objectForKey:@"gonum"];
    
    view.labRemark.text = [_dic objectForKey:@"remarks"];
    
    NSDictionary *comment = [_dic objectForKey:@"comment"];
    if ([comment isKindOfClass:[NSDictionary class]]) {
        StarView *star = [[StarView alloc] initWithFrame:CGRectMake(0, 0, 80, 16)];
        star.showStar = 0;
        [view.starView addSubview:star];
        
        view.labPJ.text = @"暂无用户评价";
    }else{
        StarView *star = [[StarView alloc] initWithFrame:CGRectMake(0, 0, 80, 16)];
        NSString *count = [[_dic objectForKey:@"comment"] objectForKey:@"star"];
        NSInteger index = [count integerValue];
        star.showStar = index * 20.0;
        [view.starView addSubview:star];
        
        view.labPJ.text = [[_dic objectForKey:@"comment"] objectForKey:@"comment"];
    }
    
    view.image1.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(click1)];
    [view.image1 addGestureRecognizer:tap1];
    
    view.image2.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap2 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(click2)];
    [view.image2 addGestureRecognizer:tap2];
    
}

- (void)click1{
    NSString *phone = [NSString stringWithFormat:@"%li",[[_dic objectForKey:@"quphone"] integerValue]];
    NSLog(@"phone:%@",phone);
    NSMutableString *str = [[NSMutableString alloc] initWithFormat:@"tel:%@",phone];
    UIWebView *web = [[UIWebView alloc] init];
    [web loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:str]]];
    [self.view addSubview:web];
}
//
- (void)click2{
    NSString *phone = [NSString stringWithFormat:@"%li",[[_dic objectForKey:@"gophone"] integerValue]];
    NSLog(@"phone:%@",phone);
    NSMutableString *str = [[NSMutableString alloc] initWithFormat:@"tel:%@",phone];
    UIWebView *web = [[UIWebView alloc] init];
    [web loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:str]]];
    [self.view addSubview:web];
}


- (void)click{
    KLDjdManViewController *man = [[KLDjdManViewController alloc] init];
    man.array = _array;
    [self.navigationController pushViewController:man animated:YES];
}
//完成详情
- (void)loadData{
    NSMutableDictionary *parmas = [NSMutableDictionary dictionary];
    [parmas setObject:_orderid forKey:@"id"];
    [parmas setObject:_merid forKey:@"merchantid"];
    NSString *url = [NSString stringWithFormat:@"%@/App/Ordermanage/cooliedetails",urlHead];
    [[AFHTTPSessionManager manager] POST:url parameters:parmas success:^(NSURLSessionDataTask *task, id responseObject) {
        NSLog(@"详情res:%@",responseObject);
        NSString *error = [NSString stringWithFormat:@"%li",[[responseObject objectForKey:@"error"] integerValue]];
        if ([error integerValue] == 200) {
            _array = [[responseObject objectForKey:@"result"] objectForKey:@"delivery"];
            _dic = [[responseObject objectForKey:@"result"] objectForKey:@"list"];
        }
        
        [self loadUI];
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        NSLog(@"error:%@",error);
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
